﻿using FI.Security;
using FI.Security.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Sample.Web.Controllers
{

    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            //throw new Exception("this is an excepiton.");
            return View();
        }
        [AllowAnonymous]
        public ActionResult NotFound(string aspxerrorpath)
        {
            ViewBag.Message = string.Format("Unable to find out the specified page / action - {0}", aspxerrorpath);

            var isAjax = this.ControllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
            if (isAjax)
            {
                return PartialView("NotFound");
            }
            return View("NotFound");
        }

        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [SitemapAuthorize(Suppress = true)]
        public ActionResult Error(string aspxerrorpath, Exception exception = null)
        {
            var warningEx = exception;

            var isAjax = this.ControllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
            string message = (HttpContext.Request.IsLocal && !isAjax)
                ? exception.ToString()
                : exception.Message;


            if (isAjax)
            {
                this.ControllerContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;//might change the status code to normal

                return Json(new Exception(message), JsonRequestBehavior.AllowGet);

            }

            ViewBag.Path = aspxerrorpath;
            ViewBag.Message = message;
            return View("Error");
        }


        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [SitemapAuthorize(Suppress = true)]
        public ActionResult ErrorPage(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                var fromBase64String = Convert.FromBase64String(message);
                message = Encoding.Default.GetString(fromBase64String);
            }

            ViewBag.Message = message;
            return View("Error");
        }



        /// <summary>
        /// 反射后台所有控制器和下面的公开action
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllMenus()
        {
            List<SitemapModel> sitemaps = new List<SitemapModel>();

            var asm = System.Reflection.Assembly.GetExecutingAssembly();

            List<Type> typeList = new List<Type>();

            var types = asm.GetTypes();

            foreach (Type type in types)
            {
                string s = type.FullName.ToLower();

                if (s.EndsWith("controller")) typeList.Add(type);
            }

            typeList.Sort(delegate (Type type1, Type type2) { return type1.FullName.CompareTo(type2.FullName); });

            foreach (Type type in typeList)
            {
                System.Reflection.MemberInfo[] members = type.FindMembers(System.Reflection.MemberTypes.Method,
                System.Reflection.BindingFlags.Public |
                System.Reflection.BindingFlags.Static |
                //System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.DeclaredOnly, Type.FilterName, "*"); //【位屏蔽】

                string controller = type.Name.Replace("Controller", "");

                foreach (var m in members)
                {
                    object[] descriptionList = m.GetCustomAttributes(typeof(AllowAnonymousAttribute), false);
                    if (descriptionList != null && descriptionList.Length > 0)
                    {
                        continue;


                    }
                    if (m.DeclaringType.Attributes.HasFlag(System.Reflection.TypeAttributes.Public) != true)
                        continue;

                    string action = m.Name;

                    if (action.Contains("<") || action.Contains(">")) continue;
                    if (action.StartsWith("get_") || action.Contains("set_")) continue;
                    SitemapModel sm = new SitemapModel();
                    sm.Controller = controller;
                    sm.Action = action;
                    sm.Id = Guid.NewGuid().ToString();
                    sm.URL = "~/" + controller + "/" + action;
                    sm.SitemapType = "Backend";
                    sm.SitemapTitle = "";
                    sm.SitemapDesc = "";
                    sm.SitemapParent = "391E97EF-769F-4F1B-AD1A-6B01E4AE8D41";
                    sm.OrderBy = 1;
                    sm.ShowInMenu = 1;
                    




                    sitemaps.Add(sm);

                }
            }
            return View(sitemaps);
        }



    }
}
